/*
 * Project name:
     LED_First (Simple 'Hello World' project)
 * Author:
     Dujany J., 2010, dujaj@hotmail.com
 * Revision History:
     13.04.2010:
       - initial release; Quick & Dirty version;
 * Description:
     This is a simple 'Hello World' project. It turns on/off LEDs and draw a spiral
 * Test configuration:
     MCU:             PIC16F887
     Dev.Board:       EasyPIC6
     Oscillator:      INTOSCIO RA6,7 as I/O, 04.0000 MHz
     Ext. Modules:    -
     Compiler:        MikroC PRO for PIC, v 3.2

 * NOTES:
     - Turn ON the PORT LEDs at SW9.
*/

int counter;

void wait() {
  Delay_ms(100);
}

void main() {

  ANSEL  = 0;                                    // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 0;

  TRISA = 0x00;                                  // set direction to be output
  TRISB = 0x00;                                  // set direction to be output
  TRISC = 0x00;                                  // set direction to be output
  TRISD = 0x00;                                  // set direction to be output

  PORTA = 0x00;                                  // turn OFF the PORTD leds
  PORTB = 0x00;                                  // turn OFF the PORTD leds
  PORTC = 0x00;                                  // turn OFF the PORTC leds
  PORTD = 0x00;                                  // turn OFF the PORTD leds


  while (1) {                                     // endless loop
    for (counter = 0; counter < 8; counter++){    // starting the spiral on PORTA
      PORTA |= 1 << counter;
      wait();
    }
    
    PORTB = 0x80;                                  // cross the PORTB
    wait();
    PORTC = 0x80;                                  // cross the PORTC
    wait();
    
    for (counter = 7; counter > -1; counter--){     // return on PORTD
      PORTD |= 1 << counter;
      wait();
    }
    
    PORTC = 0x81;                                  // cross the PORTB
    wait();
      
    for (counter = 0; counter < 7; counter++){     // return on PORTB
      PORTB |= 1 << counter;
      wait();
    }
    
    for (counter = 6; counter > 0; counter--){     // return on PORTC
      PORTC |= 1 << counter;
      wait();
    }

    counter = 0;
    while (counter < 8) {                          // loop to clear all PORT curtain style
      PORTA &= ~(1 << counter);
      PORTB &= ~(1 << counter);
      PORTC &= ~(1 << counter);
      PORTD &= ~(1 << counter);
      wait();
      counter++;
    }
  }
}
